package cn.com.gridinfo.sooc.core.login.dao;

import com.fasterxml.jackson.databind.JsonNode;
import com.jeremy.arad.utils.Base64Coder;
import com.jeremy.arad.utils.JsonUtil;
import com.jeremy.arad.utils.Log;
import com.jeremy.arad.utils.StringUtils;
import com.loopj.android.http.RequestParams;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.com.gridinfo.sooc.base.http.SoocIDao;
import cn.com.gridinfo.sooc.base.http.SoocNetResult;
import cn.com.gridinfo.sooc.common.PublicConst;
import cn.com.gridinfo.sooc.common.UrlConst;
import cn.com.gridinfo.sooc.core.login.bean.MiBaoQuestion;
import cn.com.gridinfo.sooc.core.login.bean.MiBaoSure;
import cn.com.gridinfo.sooc.core.login.bean.NodeServerInfo;

import cn.com.gridinfo.sooc.core.login.bean.ForgetUserInfo;
import cn.com.gridinfo.sooc.core.login.common.CodeConst;
import cn.com.gridinfo.sooc.utils.HttpUrl;
import cn.com.gridinfo.sooc.utils.MD5;

/**
 * 忘记密码 dao
 * Created by jieli on 15/8/24.
 */
public class ForgetDao extends SoocIDao {
    public String status = "";//确认是否成功的信息，  "0": 成功  "1": 失败
    public int status2 = 0;
    public String statusCode = "";//错误信息
    public List<NodeServerInfo> schoolInfoList = new ArrayList<>();//节点列表信息
    public ForgetUserInfo forgetUserInfo = new ForgetUserInfo();//用户信息的实体类
    public List<MiBaoQuestion> miBaoQuestion = new ArrayList<>();//密保问题实体类
    public List<MiBaoSure> miBaoSures = new ArrayList<>();//检验问题是否正确的实体类
    public int count;//节点数量
    public MD5 md5;
    public Base64Coder base64Coder;
    private String xtdz;
    public int right = -1;

    public void setXtdz(String xtdz) {
        this.xtdz = xtdz;
    }

    public ForgetDao(SoocNetResult activity) {
        super(activity);
    }


    @Override
    public void onRequestSuccess(JsonNode result, int requestCode) throws IOException {
        //0 成功，1 失败
        if (result != null) {

            if (requestCode != CodeConst.GET_MYSCHOOLLIST) {
                status = result.get("status").asText();
                statusCode = result.get("statusCode").asText();
                if (!status.equals("0")) {
                    return;
                }
            }
        } else {
            return;
        }
        Log.d("请求code：" + requestCode + "");
        Log.d("status:" + status);
        Log.d("statusCode：" + statusCode);
        Log.d("result->>" + result.toString());
        switch (requestCode) {
            case CodeConst.GET_MYSCHOOLLIST:
                count = result.get("count").asInt();

                schoolInfoList = JsonUtil.node2pojoList(result.get("lists"), NodeServerInfo.class);
                break;
            case CodeConst.FORGETPWD_INFO:

                Log.d("获取信息 status：" + status);
                Log.d("获取信息statusCode:" + statusCode);
                forgetUserInfo = JsonUtil.node2pojo(result.get("data"), ForgetUserInfo.class);
                break;
            case CodeConst.GET_FORGETYZM:
                Log.d("发送验证码status:" + status);
                Log.d("发送验证码statusCode：" + statusCode);
                Log.d("result->>" + result.toString());
                break;
            case CodeConst.YZM_SURE:
                Log.d("成功比对验证码" + "+++");
                break;
            case CodeConst.MIBAOWENTI:
                status = result.get("status").asText();
                statusCode = result.get("statusCode").asText();

                miBaoQuestion = JsonUtil.node2pojoList(result.get("data"), MiBaoQuestion.class);
                break;
            case CodeConst.SUERMIBAO:
                Log.d("statust:" + status);
                Log.d("statusCode：" + statusCode);
                Log.d("验证密保result--》》" + result.toString());
//                right=JsonUtil.node2pojo(result.get("data").get("right"),Integer.class);

                break;
            case CodeConst.NEWPASSWORD:
                Log.d("status:" + status);
                Log.d("statusCode：" + statusCode);
                Log.d("修改密码——————" + result.toString());
                break;


        }

    }

    /**
     * 找回密码时，获取学校列表
     */
    public void getMySchoolList(String account) {
        String url = PublicConst.CLOUD_URL + "/index.php?m=api&c=Index&a=getSnsList";
        RequestParams params = new RequestParams();
        params.add("account", account);
        postRequest(url, params, CodeConst.GET_MYSCHOOLLIST, PublicConst.NOT_NEED_TOKEN);
    }

    /**
     * 找回密码，显示基本信息（手机号，邮箱）
     */
    public void getForgetUserInfo(String account, String key) {
        String url = xtdz + "/index.php?s=apiv2/User/finduser";
        Log.d("url" + url);
        RequestParams params = new RequestParams();
        params.add("account", account);
        params.add("key", MD5.encode(key));
        Log.d("KEY" + MD5.encode(key));
        postRequest(url, params, CodeConst.FORGETPWD_INFO, PublicConst.NOT_NEED_TOKEN);

    }

    /**
     * 获取验证码
     *
     * @param mobile 手机号码
     * @param type   短信类型:  1:密码找回  2:注册验证
     * @param dlxtbb 登录系统版本
     * @param dlsbxh 登录设备型号
     * @param yjbsm  硬件标识码
     */
    public void getYanzhengma(String mobile, int type, String dlxtbb, String dlsbxh, String yjbsm) {
        String url = HttpUrl.getHttpUrl(xtdz, UrlConst.Module.User.toString(), "sms");
        RequestParams params = new RequestParams();
        params.add("mobile", mobile);
        params.add("type", type + "");
        params.add("dlxtbb", dlxtbb);
        params.add("dlsbxh", dlsbxh);
        params.add("yjbsm", yjbsm);
        postRequest(url, params, CodeConst.GET_FORGETYZM, PublicConst.NOT_NEED_TOKEN);
    }

    /**
     * 验证找回密码时候的验证码是否正确
     *
     * @param telPhone 手机号码
     * @param ymz      验证码
     * @param key      key(MD5)加密
     */
    public void suerYanzhengma(String telPhone, int ymz, String key) {
        String url = xtdz + "/index.php?s=apiv2/User/checktel";
        RequestParams params = new RequestParams();
        params.add("telPhone", telPhone);
        params.add("yzm", ymz + "");
        key = MD5.encode(key);
        params.add("key", key);
        Log.d("比对验证码key：" + key);
        postRequest(url, params, CodeConst.YZM_SURE, PublicConst.NOT_NEED_TOKEN);


    }

    /**
     * 重置密码
     *
     * @param uid         用户id
     * @param newpassword 新密码
     * @param type        找回方式
     * @param key         校验
     */
    public void forgetPwd(String uid, String newpassword, String type, String key) {
        String url = xtdz + "/index.php?s=apiv2/User/resetuser";
        Log.d("url" + url);
        RequestParams params = new RequestParams();
        params.add("uid", uid);
        params.add("newpassword", MD5.encode(StringUtils.changeSpecialChar(newpassword)));
        Log.d("转译后=" + MD5.encode(StringUtils.changeSpecialChar(newpassword)));
        params.add("type", type);
        params.add("key", MD5.encode(key));
        Log.d("newpass=" + newpassword);
        Log.d("csnshu="+params.toString());
        postRequest(url, params, CodeConst.NEWPASSWORD, PublicConst.NOT_NEED_TOKEN);
    }

    /**
     * 重置密码(邮箱）
     *
     * @param uid  用户id
     * @param type 找回方式
     * @param key  校验
     */
    public void forgetPwdByEmail(String uid, String type, String key) {
        String url = xtdz + "/index.php?s=apiv2/User/resetuser";
        RequestParams params = new RequestParams();
        params.add("uid", uid);
        params.add("type", type);
        params.add("key", MD5.encode(key));
        postRequest(url, params, CodeConst.NEWPASSWORDBYEMAIL, PublicConst.NOT_NEED_TOKEN);
    }

    /**
     * 获得密保找回的问题
     *
     * @param uid 用户id
     * @param key 校验
     */
    public void mibaoQuestion(String uid, String key) {
        String url = xtdz + "/index.php?s=apiv2/User/getquestion";
        RequestParams params = new RequestParams();
        params.add("uid", uid);
        params.add("key", MD5.encode(key));
        postRequest(url, params, CodeConst.MIBAOWENTI, PublicConst.NOT_NEED_TOKEN);
    }

    /**
     * 验证密保问题
     *
     * @param uid  用户id
     * @param data json数据
     * @param key  校验
     */
    public void mibaoSure(String uid, String data, String key) {
        String url = xtdz + "/index.php?s=apiv2/User/checkquestion";
        RequestParams params = new RequestParams();
        params.add("uid", uid);
        data = Base64Coder.encodeString(data);
        params.add("data", data);
        key = MD5.encode(uid + data + "0S3FV7PH8MLJT6IOAG2ECWQNXUBR1YZ459KD");
        params.add("key", key);
        postRequest(url, params, CodeConst.SUERMIBAO, PublicConst.NOT_NEED_TOKEN);

        Log.d("uid" + uid);
        Log.d("data" + data);
        Log.d("key" + key);

    }


    //返回节点列表信息
    public List<NodeServerInfo> getSchoolInfoList() {
        return schoolInfoList;
    }

    //获取网络请求错误的内容信息
    public String getStatusCode() {
        return statusCode;
    }
}
